package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.CraftsmanHealthQO;
import com.xingkeduo.pojo.table.CraftsmanHealthTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 发型师健康证
 *
 * @author : gaozifeng
 * @date : 2019/3/20 15:46
 */
@Component
public class CraftsmanHealthJTO extends BaseJson {
    public static CraftsmanHealthQO createQueryForPrimary(String json) throws Throwable {
        CraftsmanHealthQO query = new CraftsmanHealthQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validId(root, YesNoEnum.YES));
        return query;
    }

    public static CraftsmanHealthTO createTableForModify(String json) throws Throwable {
        CraftsmanHealthTO table = new CraftsmanHealthTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        if(root.has("effectiveTime") && root.has("finishTime")){
            table.setEffectiveTime(validDate(root, YesNoEnum.NO, "effectiveTime")).setFinishTime(validDate(root, YesNoEnum.NO, "finishTime")).setHealthNum(validString(root, "healthNum", 50, YesNoEnum.NO));
        }
        return table;
    }

    public static CraftsmanHealthQO createQueryForList(String json) throws Throwable {
        CraftsmanHealthQO query = new CraftsmanHealthQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setCraftsman_id(validCraftsmanId(root, YesNoEnum.YES));
        return query;
    }

    public static ObjectNode toJson(CraftsmanHealthTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        //status 1有效 2即将过期 3过期
        if (null != table) {
            root.put("craftsmanId", table.getCraftsman_id());
            root.put("healthNum", table.getHealthNum());
            long finishTime = table.getFinishTime().getTime();
            long nowTime = System.currentTimeMillis();
            long endDay = (finishTime - nowTime) / 86400000;
            if (finishTime > nowTime) {
                if (endDay > 30) {
                    //有效
                    root.put("healthStatus", "1");
                } else {
                    //即将过期
                    root.put("healthStatus", "2");
                }
            } else {
                //过期
                root.put("healthStatus", "3");
            }
            SimpleDateFormat formatTimestamp = new SimpleDateFormat("yyyy-MM-dd");
            root.put("effectiveTime", formatTimestamp.format(table.getEffectiveTime()));
            root.put("finishTime", formatTimestamp.format(table.getFinishTime()));
        }
        toBaseJson(root, table);
        return root;
    }

    public static ArrayNode toJson(List<CraftsmanHealthTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<CraftsmanHealthTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }

        return root;
    }


    public static long validCraftsmanId(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "craftsmanId";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (value.length() > 20)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Long.parseLong(value);
        }
        return 0L;
    }

    public static Date validDate(JsonNode node, YesNoEnum isRequired, String key) throws Throwable {
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        String value = node.path(key).asText().trim();
        SimpleDateFormat formatTimestamp = new SimpleDateFormat("yyyy-MM-dd");
        return formatTimestamp.parse(value);
    }
}
